<?php
namespace frontend\controllers;
use yii;
use frontend\models\Jobs;
use frontend\models\Recruit_user;
use frontend\models\Talk;
use yii\web\Controller;
use yii\web\Cookie;
/**
 * Site controller
 */
class TalkController extends Controller
{
	public $layout='index';
	//首页
	public function actionIndex(){
		
		//判断是否登录
		$cookie = Yii::$app->request->cookies;
		//直接返回Cookie的值
		$user = $cookie->getValue('user'); //$cookie[‘smister’] 其实这样也是可以读取的
		// var_dump($user);die;
		// print_r($user);die;
		//判断一个Cookie是否存在
		if ( $user  ) {
			//到首页
			return $this->render('index');
		}else{
			//到登录
			echo "<script>alert('请先登录');location.href='http://www.zhangsan.com/Nine/advanced/frontend/web/?r=talk/login'</script>";
		}
		
	}
	//登录
	public function actionLogin(){
		//实例化model类
		$model = new Recruit_user();
		//实例化验证类
		$request = Yii::$app->request;
		//接收数据
		$post = $request->post(); 
		if (Yii::$app->request->isPost && $model->load($post)) {

			$result = Yii::$app->db->createCommand("SELECT * FROM Recruit_user where username='".$post['Recruit_user']['username']."' and password='".$post['Recruit_user']['password']."' ")->queryOne();

			// print_r($result);die;
            if ($result) {
            	// 添加cookie 操作
				$cookies = Yii::$app->response->cookies;
				// var_dump($result);die;
				$id = $result['id'];
				
				$cookie_data = ['name' => 'user', 'value' => $id];
				// print_r($cookie_data);die;
				$cookies->add(new Cookie($cookie_data));

            	//第一种方法
				// $cookie = new \yii\web\Cookie();
				// $cookie -> name = 'user';        //cookie的名称
				// $cookie -> expire = time() + 3600*24*7;	   //存活的时间
				// $cookie -> httpOnly = true;		   //无法通过js读取cookie
				// $cookie -> value = $result;	   //cookie的值
				// Yii::$app->response->getCookies()->add($cookie);

            	$this->redirect('?r=talk/index');
            }else{
            	echo "<script>alert('账号或密码输入错误');location.href='http://www.zhangsan.com/Nine/advanced/frontend/web/?r=talk/login'</script>";
            }
		}else{
			return $this->render('login',['model' => $model]);
		}
	}
	//注册
	public function actionReg(){
		//实例化model类
		$model=new Recruit_user();
		//实例化验证类
		$request = Yii::$app->request;
		//接收数据
		$post = $request->post(); 

		
		if (Yii::$app->request->isPost && $model->load($post)) {
			

			$model->password = $post['Recruit_user']['password'];
	        $model->username = $post['Recruit_user']['username'];
	        $model->save(false);

	        $id = Yii::$app->db->getLastInsertID();
			//添加cookie 操作
			$cookies = Yii::$app->response->cookies;
			$cookie_data = ['name' => 'user', 'value' => $id];
			// print_r($cookie_data);die;
			$cookies->add(new Cookie($cookie_data));
	        
	        $this->redirect('?r=talk/index');
		}else{
			//返回到视图层字段
	     	return $this->render('reg', ['model' => $model]);
		}
	}
	
	//退出
	public function actionLoginout()
	{
		//cookie响应
		$cookies = Yii::$app->response->cookies;
		$cookies->remove('user');
		$this->redirect('?r=talk/login');
	}

	//全部职位
	public function actionJobs(){
		$model = new Jobs();
		$data = $model->find()->asarray()->all();
		// print_r($data);die;
		// $data = Jobs::findOne($user1);
		$map['data'] = $data;
		return $this->render('jobs',$map);
	}
	//社会招聘
	public function actionSocial(){
		return $this->render('social');
	}
	//公司介绍
	public function actionAbout(){
		return $this->render('about');
	}
	//联系我们
	public function actionContact(){
		return $this->render('contact');
	}
}


?>